﻿Imports System.Xml
Imports System.IO
Imports System.Net
Imports System.Web
Module Weather
    Dim doc As XmlDocument
    Dim ns As XmlNamespaceManager
    Dim nodes As XmlNodeList
    Dim code As String
    Sub GetRegion(ByVal ZipCode As String)
        ' Create a new XmlDocument  
        doc = New XmlDocument()
        ' Load data  
        doc.Load("http://xml.weather.yahoo.com/forecastrss?p=" & ZipCode)
        ' Set up namespace manager for XPath  
        ns = New XmlNamespaceManager(doc.NameTable)
        ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0")
        ' Get forecast with XPath  
        nodes = doc.SelectNodes("/rss/channel/yweather:location", ns)
        For Each node As XmlNode In nodes
            Form1.Label2.Text = node.Attributes("city").InnerText & ", " _
            & node.Attributes("region").InnerText & " " & ZipCode + " - " _
            + node.Attributes("country").InnerText
        Next
    End Sub
    Sub GetWeather(ByVal ZipCode As String)
        ' Create a new XmlDocument  
        doc = New XmlDocument()
        ' Load data  
        doc.Load("http://xml.weather.yahoo.com/forecastrss?p=" & ZipCode)
        ' Set up namespace manager for XPath  
        ns = New XmlNamespaceManager(doc.NameTable)
        ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0")
        ' Get forecast with XPath  
        nodes = doc.SelectNodes("/rss/channel/item/yweather:condition", ns)
        For Each node As XmlNode In nodes
            code = node.Attributes("code").InnerText
            Form1.lblcc.Text = node.Attributes("text").InnerText & ", " & node.Attributes("temp").InnerText & "F"
            Form1.PictureBox1.ImageLocation = "http://l.yimg.com/a/i/us/we/52/" & code & ".gif"
        Next
    End Sub
    Sub GetForecast(ByVal ZipCode As String)
        ' Create a new XmlDocument  
        doc = New XmlDocument()
        ' Load data  
        doc.Load("http://xml.weather.yahoo.com/forecastrss?p=" & ZipCode)
        ' Set up namespace manager for XPath  
        ns = New XmlNamespaceManager(doc.NameTable)
        ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0")
        ' Get forecast with XPath  
        nodes = doc.SelectNodes("/rss/channel/item/yweather:forecast", ns)
        For Each node As XmlNode In nodes
            Form1.lblforecast.Text = node.Attributes("day").InnerText + ": " _
            + node.Attributes("text").InnerText + ", " + node.Attributes("low").InnerText + "F - " _
            + node.Attributes("high").InnerText + "F"
        Next
    End Sub

    Public Sub PostTweet(ByVal username As String, ByVal password As String, ByVal tweet As String)
        If tweet.Length > 140 Then
            MsgBox("Tweet is over 140 Characters.", MsgBoxStyle.Exclamation, "Characters Exceeded")
        ElseIf tweet.Length <= 140 Then
            Try
                ' encode the username/password
                Dim user As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes((username & ":") + password))
                ' determine what we want to upload as a status
                Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes("status=" & tweet)
                ' connect with the update page
                Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://twitter.com/statuses/update.xml"), HttpWebRequest)
                ' set the method to POST
                request.Method = "POST"
                request.ServicePoint.Expect100Continue = False
                ' thanks to argodev for this recent change!
                ' set the authorisation levels
                request.Headers.Add("Authorization", "Basic " & user)
                request.ContentType = "application/x-www-form-urlencoded"
                ' set the length of the content
                request.ContentLength = bytes.Length
                ' set up the stream
                Dim reqStream As Stream = request.GetRequestStream()
                ' write to the stream
                reqStream.Write(bytes, 0, bytes.Length)
                ' close the stream
                reqStream.Close()
            Catch ex As Exception

            End Try
        End If
    End Sub
End Module